home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / mm / ccmd / filelist.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-18  |  21.3 KB  |  813 lines

  1. /*
  2.  Copyright (c) 1986, 1990 by The Trustees of Columbia University in
  3.  the City of New York.  Permission is granted to any individual or
  4.  institution to use, copy, or redistribute this software so long as it
  5.  is not sold for profit, provided this copyright notice is retained.
  6.  
  7.  Author: Howie Kaye
  8. */
  9. /*
  10.  * Given a vector of directories, build a table of all files in those
  11.  * directories
  12.  * If the path passed here is the same as the previous one (help, and the 
  13.  * completion, uses previous listing for speed.
  14.  *
  15.  * All buffers are either malloc'ed or realloc'ed when needed.  If 
  16.  * these fail, bad things can happen -- i'm not yet sure what to do in this 
  17.  * case.
  18.  *
  19.  */
  20.  
  21. #include "ccmdlib.h"
  22. #include "filelist.h"
  23.  
  24. #ifdef NODIRLIB
  25. typedef struct {
  26.     int size;
  27.     struct direct *files;
  28.     struct direct *current;
  29. } DIR;
  30. #endif
  31.  
  32. #if defined(ns32000) && defined(BSD)
  33. #undef rewinddir
  34. #define rewinddir(dirp) seekdir((dirp), (long)0)
  35. DIR *OPENDIR(fn) { DIR *d = opendir(fn); d->dd_hashed_dir = 0; return(d); }
  36. #else
  37. #define OPENDIR(fn) opendir(fn)
  38. #endif
  39.  
  40.  
  41.  
  42. #ifdef DIRENTLIB
  43. #define direct dirent            /* XXX kludge */
  44. #endif
  45.  
  46. #if unix
  47. #ifndef _CMUSR
  48. #include <pwd.h>            /* for "~" completion */
  49. #if SYSV
  50. struct passwd *getpwent(), *getpwnam(), *getpwuid();
  51. #endif /* SYSV */
  52. #endif /*  !_CMUSR */
  53. #endif /*  unix */
  54.  
  55. /* make sure toupper does what we want */
  56. #undef toupper
  57. #define toupper(c) (islower(c) ? (c)-'a'+'A' : (c))
  58. #define BUFLEN 120
  59.  
  60. char *tilde_expand();
  61.  
  62. char *namebuf;                /* hold filenames here */
  63. char *prevnamebuf;            /* previous name buffer */
  64.  
  65. dirfile *files;                /* list of files */
  66. dirfile *prevfiles;            /* list from previous pass */
  67.  
  68. dirfile **fileindex;            /* vector of files for sorting */
  69. dirfile **previndex;            /* index from previous pass */
  70.  
  71. int filecount;                /* number of files entries used */
  72. int prevfilecount;            /* from previous pass */
  73. int maxfiles;                /* number of file entries malloc'ed */
  74.  
  75. char *curfile;                /* current file in namelist */
  76. int namelen;                /* used length of namebuf */
  77. int maxnamelen;                /* malloc'ed length of namebuf */
  78. char **prevpath=NULL;            /* previous search path  */
  79. char **usepath=NULL;            /* expanded path to use */
  80. int currentpath= 0;            /* current spot in usepath */
  81. int usepathlen=0;
  82. int redofiles = TRUE;
  83. int *modtimes = NULL;
  84. int suffix_modtime = 0;
  85. char *oldsuffix = NULL;
  86.  
  87. char *malloc(), *realloc(), *index();
  88. void qsort();                /* quick sort routine */
  89.  
  90. int dirfilecmp();            /* predicate for qsort */
  91.  
  92. /*
  93.  * chng_suffix:
  94.  * determine whether or not suffix has changed.  if it has, need
  95.  * to reread the directory.
  96.  * if suffix and oldsuffix are not equal, or they are equal but the modtimes
  97.  * are different, return TRUE, else return FALSE
  98.  */
  99.  
  100. chng_suffix (suffix, oldsuffix)
  101. char *suffix, *oldsuffix;
  102. {
  103.   struct stat sbuf;
  104.  
  105.   if (strcmp (suffix, oldsuffix) != 0)
  106.     return (TRUE);
  107.   if (stat (suffix, &sbuf) != 0)    /* don't know, be safe */
  108.     return (TRUE);
  109.   if (suffix_modtime == sbuf.st_mtime)
  110.     return (FALSE);
  111.   suffix_modtime = sbuf.st_mtime;
  112.   return (TRUE);
  113. }
  114.  
  115.  
  116. /*
  117.  * cmfileini:
  118.  * (re)initialize file vars.  Useful in making new file appear for
  119.  * file parse.
  120.  */
  121. cmfileini()
  122. {
  123.     redofiles = TRUE;
  124. }
  125.  
  126. /*
  127.  * search_path:
  128.  * takes a NULL terminated vector of directories.
  129.  * returns a pointer to a listing of the files.
  130.  * this listing is guaranteed to stay intact until the function is
  131.  * called again.
  132.  * fcount is set to the number of files returned
  133.  */
  134.  
  135. dirfile **
  136. search_path(path,suffix,fcount) char **path; char *suffix; int *fcount; {
  137.   int pathlen;                /* length of the path */
  138.   char **p;
  139.   int i;
  140.   register char **p1, **p2;        /* as last time */
  141.   struct stat sbuf;
  142.   int *m,goodlen = 0;
  143.  
  144.   p1 = path;
  145.   p2 = prevpath;
  146.   m = modtimes;
  147.  
  148.   if ((suffix == NULL && oldsuffix == NULL) ||
  149.       (suffix != NULL && oldsuffix != NULL 
  150.        && !chng_suffix (suffix, oldsuffix)))
  151.       {
  152.       for (;!redofiles;) {        /* check if it is same path */
  153.       if (p1 == NULL || p2 == NULL) break; /* NULL list? */
  154.       if (*p1 == NULL && *p2 != NULL) break; /* one null? */
  155.       if (*p1 != NULL && *p2 == NULL) break;
  156.       if ((*p1 != NULL) && (*p2 != NULL))
  157.           if (strcmp(*p1,*p2))
  158.           break;        /* or different entry...rebuild list */
  159.       if (*p1 == NULL) {        /* end of list?  they are the same */
  160.                     /* same list as last time...so */
  161.           namebuf = prevnamebuf;    /* make everything point to last set*/
  162.           *fcount = filecount = prevfilecount;
  163.           files = prevfiles;
  164.           fileindex = previndex;
  165.           return(previndex);    /* and return prev index */
  166.       }
  167.       if (stat(*p1,&sbuf) != 0)    /* if a directory changed, rebuild */
  168.           break;
  169.       if (sbuf.st_mtime != *m) break;
  170.       p1++,p2++;                /* keep walking the list */
  171.       }
  172.   }
  173.   redofiles = FALSE;
  174.                     /* different path...free up old */
  175.                     /* buffers */
  176.  
  177.   if (prevnamebuf != NULL)        /* free old name buffer */
  178.     free(prevnamebuf);
  179.   if (prevfiles != NULL)        /* and old file list */
  180.     free(prevfiles);
  181.   if (previndex != NULL)        /* old index */
  182.     free(previndex);
  183.   if (prevpath != NULL) {        /* and old path */
  184.     for (p = prevpath; *p != NULL; p++)    /* have to free each name in path */
  185.       free(*p);
  186.     free(prevpath);
  187.   }
  188.   if (modtimes != NULL)
  189.       free(modtimes);
  190.   filecount = prevfilecount = 0;    /* no previous files */
  191.   fileindex = NULL;            /* or current files */
  192.   files = NULL;
  193.   namebuf = NULL;
  194.   curfile = NULL;
  195.   namelen = 0;
  196.   maxnamelen = 0;
  197.   maxfiles = 0;
  198.                     /* now copy the current path */
  199.   pathlen = 0;
  200.   for (p = path; *p != NULL; p++)    /* first find it's length */
  201.     pathlen++;
  202.   prevpath = (char **) malloc((pathlen+1)*sizeof(char *)); /* build vector */
  203.   modtimes = (int *) malloc((pathlen)*sizeof(int));
  204.   for (i = 0,p = path; *p != NULL; i++,p++) { /* then copy the names */
  205.     prevpath[i] = malloc((1+strlen(*p)) * sizeof(char));
  206.     strcpy(prevpath[i],*p);
  207.     if (stat(*p,&sbuf) == 0)
  208.     modtimes[i] = sbuf.st_mtime;
  209.     else modtimes[i] = -1;
  210.   }
  211.   prevpath[pathlen] = NULL;        /* NULL termination */
  212.  
  213.   if (usepath != NULL) {        /* and expanded path */
  214.     for (i = 0,p = usepath; i < usepathlen;p++,i++) {
  215.       if (*p != NULL) free(*p);
  216.     }
  217.     free(usepath);
  218.   }
  219.   usepath = (char **) malloc((pathlen+1)*sizeof(char *)); /* build vector */
  220.   usepathlen=pathlen;            /* and remember length */
  221.  
  222.   goodlen = 0;
  223.   for (i = 0; i < usepathlen; i++) {
  224.     int xlen = 0;
  225.  
  226.     if (suffix) 
  227.     xlen = strlen(suffix);
  228.                     /* if it starts with a /, use */
  229.     if (suffix && (suffix[0] == '/' || strlen(suffix) == 0 ||
  230.            suffix[0] == '~' || (strcmp(suffix,".") == 0) ||
  231.            (strcmp(suffix,"..") == 0))) {    
  232.       int j;                /* as an absolute path */
  233.       char *term = "";
  234.       if (suffix[strlen(suffix)-1] != '/')
  235.     term = "/";
  236.       usepath[i] = malloc(xlen + 2);
  237.       sprintf(usepath[i],"%s%s",suffix,"/");
  238.       goodlen = 1;
  239.       for (j = 1; j < usepathlen; j++)
  240.     usepath[j] = NULL;
  241.       break;
  242.     }
  243.     else {
  244.       usepath[i] = malloc((1+xlen+sizeof("/")+
  245.                strlen(prevpath[i]))*sizeof(char));
  246.       strcpy(usepath[i],prevpath[i]);
  247.       if (suffix) {
  248.     if (usepath[i][strlen(usepath[i])-1] != '/')
  249.         strcat(usepath[i], "/");
  250.     strcat(usepath[i],suffix);
  251.       }
  252.       if (stat(usepath[i],&sbuf) == 0)
  253.       goodlen++;
  254.     }
  255.   }
  256.   usepath[usepathlen] = NULL;
  257.   for (i =0; i < usepathlen; i++) {    /* now build up list */
  258.     currentpath = i;
  259.     if (usepath[i] != NULL && modtimes[i] != -1) 
  260.     search_dir(usepath[i],goodlen <= 1);
  261.   }
  262.                     /* build up index into list */
  263.   if (oldsuffix)
  264.       free(oldsuffix);
  265.   if (suffix) {
  266.     struct stat sbuf;
  267.  
  268.     oldsuffix = malloc(strlen(suffix)+1);
  269.     strcpy(oldsuffix,suffix);
  270.     if (stat(suffix,&sbuf) == 0)
  271.       suffix_modtime = sbuf.st_mtime;
  272.   }
  273.   else oldsuffix = NULL;
  274.   fileindex = (dirfile **) malloc(sizeof(dirfile *) * filecount);
  275.   for (i = 0; i < filecount; i++) fileindex[i] = &files[i];
  276.                     /* and sort it */
  277.   qsort(fileindex,filecount,sizeof(dirfile *), dirfilecmp);
  278.  
  279.   previndex = fileindex;            /* save these for next time */
  280.   prevnamebuf = namebuf;
  281.   prevfiles = files;
  282.   *fcount = prevfilecount = filecount;
  283.   return(previndex);
  284. }
  285.  
  286. /*
  287.  * search_dir:
  288.  * builds up a file list for a single directory
  289.  * if a directory is wild, expands it.
  290.  * if directory is CWD, then includes ".", and ".."
  291.  */
  292.  
  293. DIR *OPENDIR();
  294.  
  295. search_dir(dirname,showdot) char *dirname; int showdot; {
  296.   struct stat sbuf;
  297.   DIR *dirp, *dotdirp;            /* directory pointers */
  298.   register struct direct *d;        /* file entries in a dir */
  299.   struct direct *dotd, *readdir();
  300.   int i;
  301.   char *dirr=dirname;            /* actual directory name to use */
  302.   struct passwd *user;            /* passwd structure for "~" files */
  303.   int ispwd;                /* flag if looking at connect dir */
  304.   register dirfile *f;
  305.   int dirstrlen;
  306. #ifdef MSDOS
  307.   static char pwdbuf[51];
  308.   char *getcwd();
  309.   int ustrcmp();
  310. #endif
  311.   if (iswild(dirname)) {
  312.     expandwilddir(dirname);    
  313.     return;
  314.   }
  315. #if unix
  316.   if (dirname[0] == '~') {        /* "~user"? */ 
  317.     dirr = tilde_expand(dirname);    /* expand it */
  318.   }
  319. #endif
  320.   dirstrlen = strlen(dirr);
  321.   ispwd = !strcmp(dirr,".");        /* if looking at "." then pwd */
  322.  
  323.                     /* otherwise, we have to check */
  324.   if (stat(dirr,&sbuf) != 0)
  325.       return;
  326.   if (!((sbuf.st_mode & S_IFMT) == S_IFDIR)) {
  327.       return;
  328.   }
  329.   if (!ispwd) {
  330. #ifdef MSDOS
  331.     if (index(STRUCTTERM,dirr[strlen(dirr)-1])) { /* on msdos, */
  332.       char buf[70];            /* check if the directory is */
  333.       int unit;                /* the same as the connected */
  334.       union REGS sregs, oregs;        /* by doing a DOS call to get the */
  335.       struct SREGS segregs;        /* connected dir */
  336.       int i;
  337.       
  338.       segread(&segregs);        /* set segment registers */
  339.       unit = 0;                /* use default structure */
  340.       if (isalpha(*dirr))
  341.     unit = toupper(*dirr) - 'A' + 1; /* no use the specified struct */
  342.  
  343.       strcpy(buf,dirr);            /* copy the directory name */
  344.       strcat(buf,"/");            /* add a "/" */
  345.       sregs.h.ah = 0x47;        /* DOS int get cwd */
  346.       sregs.x.si = (int) &buf[strlen(buf)];
  347.       sregs.h.dl = unit;
  348.       intdosx(&sregs, &oregs, & segregs); /* do the interrupt */
  349.       for (i = 0; i < strlen(buf); i++)    /* and lowercase the string */
  350.     if (isupper(buf[i])) buf[i] = tolower(buf[i]);
  351.       dirp = OPENDIR(buf);        /* open up directory */
  352.     }
  353.     else
  354.       dirp = OPENDIR(dirr);        /* open directory we are looking at */
  355. #else
  356.     dirp = OPENDIR(dirr);        /* open directory we are looking at */
  357. #endif
  358.     if (dirp == NULL) {
  359.     perror(dirr);
  360.     return;                /* can't...just return */
  361.     }
  362.     if (defstruct(dirr)) {        /* same structure? */
  363.       dotdirp = OPENDIR(".");        /* open up "." */
  364.       if (dotdirp != NULL) {
  365.     d = readdir(dirp);        /* read dir entries */
  366.     dotd = readdir(dotdirp);
  367.     if (d != NULL && dotd != NULL)
  368. #if unix
  369.       ispwd = (d->d_ino == dotd->d_ino); /* are they the same? */
  370. #endif
  371. #ifdef MSDOS
  372.           ispwd = (!ustrcmp(d->d_name,getcwd(pwdbuf,50)));
  373. #endif
  374.     closedir(dotdirp);
  375.         rewinddir(dirp);
  376.       }
  377.       else {
  378.     perror(".");
  379.       }
  380.     }                    /* if different struct, different */
  381.   }
  382.   else {
  383.     dirp = OPENDIR(dirr);        /* open up directory */
  384.     if (dirp == NULL) { 
  385.       perror(dirr);
  386.       return;                /* couldn't...go home */
  387.     }
  388.   }
  389.                     /* directory is now open */
  390.   if (!ispwd && !showdot) {        /* if not CWD, skip ".", ".." */
  391.       do d = readdir (dirp);
  392.       while (d && d->d_name[0] == '.' &&
  393.          (d->d_name[1] == 0 ||
  394.           (d->d_name[1] == '.' && d->d_name[2] == 0)));
  395.   }
  396.   else {
  397.       d = readdir(dirp);
  398.   }
  399.   if (d == NULL) {
  400.       perror("readdir");
  401.   }
  402.   for (; d != NULL; d = readdir(dirp)) {
  403.     int fnlength;
  404.                     /* do we need more files? */
  405.  
  406.     if (filecount == maxfiles) {
  407.       maxfiles += FILEINCR;
  408.       files =(dirfile *)cmrealloc(files, sizeof(dirfile)*(maxfiles));
  409.     }
  410.                     /* do we need more name space  */
  411. #if MSDOS || defined(NDIRLIB) || defined(DIRENTLIB)
  412.     fnlength = strlen(d->d_name);
  413. #else /* MSDOS */
  414. #if BSD
  415.     fnlength = d->d_namlen;
  416. #else
  417.     fnlength = strlen(d->d_name);
  418.     if (fnlength > 14)
  419.     fnlength = 14;
  420. #endif /* !BSD */
  421. #endif /* !MSDOS */
  422.     if (maxnamelen - (curfile - namebuf) < (fnlength + 1)) {
  423.       int offset = curfile - namebuf;
  424.       maxnamelen += NAMEINCR;
  425.       namebuf = cmrealloc(namebuf,sizeof(char)*maxnamelen);
  426.       curfile = namebuf + offset;
  427.     }
  428.     f = &files[filecount];        /* set up pointer to current file */
  429.     f->offset = curfile-namebuf;    /* it's offset into namebuf */
  430.  
  431.     strncpy(curfile, d->d_name, fnlength);
  432.     curfile[fnlength] = 0;
  433.     curfile += fnlength + 1;        /* bump current pointer by namelen */
  434.  
  435.     f->directory = dirname;        /* point to it's directory */
  436.     f->flags = 0;            /* no flags */
  437.     filecount++;
  438.   }
  439.   closedir(dirp);
  440. }
  441.   
  442.  
  443. /*
  444.  * dirfilecmp:
  445.  * predicate to compare two dirfile struct's for qsort
  446.  */
  447. dirfilecmp(a,b) register dirfile **a,**b; {
  448.   return(strcmp(&namebuf[(*a)->offset],&namebuf[(*b)->offset]));
  449. }
  450.  
  451.  
  452. /*
  453.  * ustrcmp:
  454.  * case insensitive string comparison
  455.  */
  456.  
  457. ustrcmp(s1,s2) char *s1, *s2; {
  458.   register char c1, c2;
  459.   for (;;s1++,s2++) {
  460.     c1 = toupper(*s1);
  461.     c2 = toupper(*s2);
  462.     if (c1 < c2) return(-1);
  463.     if (c1 > c2) return(1);
  464.     if (c1 == '\0') return(0);
  465.   }
  466. }
  467.  
  468. /* 
  469.  * expand the current directory, and place expansion in 
  470.  * usepath vector after current loc.
  471.  * expand usepath as we go.
  472.  */
  473.  
  474. expandwilddir(dirname) char *dirname; {
  475.   DIR *dirp;
  476.   struct direct *d;
  477.   static char basename[BUFLEN],fname[BUFLEN],rest[BUFLEN];
  478.   char c;
  479.   static char temp[BUFLEN];
  480.   
  481.   int i,base;
  482.   struct stat s;
  483.   
  484.   base = -1;                /* find the non wild base */
  485.   for (i = 0; i < strlen(dirname); i++) {
  486.     c=dirname[i];
  487.     if (index(DIRSEP,c)) {
  488.       base = i;
  489.       continue;
  490.     }
  491.     if (index(WILDCHARS,c)) break;
  492.   }
  493.   if (base == -1) {            /* no base dir.   use dot */
  494.     strcpy(basename,".");
  495.   }
  496.   else {                /* a base dir.  use it */
  497.     strncpy(temp,dirname,base);
  498.     temp[base] = '\0';
  499.     strcpy(basename,tilde_expand(temp));
  500.   }
  501.  
  502.   if (strlen(basename) == 0) strcpy(basename,"/");
  503.  
  504.   fname[0] = '\0';            /* the current wild part */
  505.   for (i = base+1; i < strlen(dirname); i++) {
  506.     if (index(DIRSEP,dirname[i])) break;
  507.     fname[i-(base+1)] = dirname[i];
  508.   }
  509.   fname[i-(base+1)] = '\0';
  510.  
  511.   i++;                    /* get past dirsep */
  512.   if (i < strlen(dirname))
  513.     strcpy(rest,&dirname[i]);        /* and the rest of the string */
  514.   else rest[0] =  '\0';
  515.   if (rest[strlen(rest)-1] == '/') rest[strlen(rest)-1] = '\0';
  516.  
  517.   if (strcmp(basename,"/") && strcmp(basename,".") && strcmp(basename,"..")) {
  518.                         /* we know these are dirs */
  519.     if (stat(basename,&s) == -1) {    /* and root breaks the msdos stat */
  520.       fprintf(stderr,"stat(%s) failed\n",basename); /* function */
  521.       return;
  522.     }
  523.     if (!((s.st_mode & S_IFMT) == S_IFDIR)) {
  524.       fprintf(stderr,"%s not a dir\n",basename);
  525.       return;
  526.     }
  527.   }
  528.   dirp = OPENDIR(basename);
  529.   if (dirp == NULL) return;
  530.                     /* insert directories into path */
  531.   d = readdir(dirp);            /* kill off . and .. */
  532.   if (!strcmp(d->d_name,"."))
  533.     readdir(dirp);
  534.   else rewinddir(dirp);
  535.   for(d = readdir(dirp); d!= NULL; d = readdir(dirp)) {
  536.     if ((d->d_name[0] != '.') || (d->d_name[0] == '.' && fname[0] == '.')) {
  537.       if (fmatch(d->d_name,fname)) {    /* if we match the wildcard */
  538.     static char buf[BUFLEN];
  539.     if (!strcmp(basename,".")) 
  540.       basename[0] = '\0';
  541.     else {
  542.       strcpy(buf,basename);
  543.     }
  544.     if (strlen(buf) > 0)
  545.       if (strcmp(buf,"/")) strcat(buf,"/");
  546.     strcat(buf,d->d_name);
  547.     if (strcmp(buf,".") && strcmp(buf,"..")) {
  548.       if (stat(buf,&s) == -1) continue;
  549.       if (!((s.st_mode & S_IFMT) == S_IFDIR)) continue;
  550.     }
  551.     if (strlen(rest) > 0) {
  552.       strcat(buf,"/");
  553.       strcat(buf,rest);
  554.     }
  555.     addtopath(buf);            /* add to the path vector */
  556.       }
  557.     }
  558.   }
  559.   closedir(dirp);            /* all done */
  560. }
  561.  
  562. /*
  563.  * actually add directories to the path in use.
  564.  */
  565. addtopath(dirname) char *dirname; {
  566.   int i;
  567.  
  568.                     /* grow the path */
  569.   usepath =(char**) cmrealloc(usepath,(usepathlen+2)*sizeof(char *));
  570.                     /* move everything else down one */
  571.   for (i = usepathlen; i > currentpath; i--) {
  572.     usepath[i+1] = usepath[i];
  573.   }
  574.   usepathlen++;                /* make space for this string */
  575.   usepath[currentpath+1] = malloc(strlen(dirname)+1);
  576.   strcpy(usepath[currentpath+1],dirname); /* and copy it in */
  577. }
  578.  
  579. #ifdef notdef
  580. /*
  581.  * index function
  582.  * returns index of char c in string s
  583.  */
  584. char *
  585. index(sp,c)                /* get index into a string */
  586. register char *sp, c;
  587. {
  588.   for (; *sp && *sp != c; sp++);
  589.   if (*sp == '\0') return(NULL);
  590.   else return(sp);
  591. }
  592. #endif
  593.  
  594. /*
  595.  * routine to check if a string is wild
  596.  * match all chars in string against all wild chars
  597.  */
  598.  
  599. iswild(str) char *str; {
  600.   int hit[128],i;
  601.   int len1, len2;
  602.   for (i = 0; i < 128; i++) hit[i] = 0;    /* no hits yet */
  603.   len1 = strlen(str);
  604.   for(i = 0; i < len1; i++)        /* mark hits */
  605.     hit[str[i]] = 1;
  606.   len2 = strlen(WILDCHARS);
  607.   for (i = 0; i < len2; i++)        /* and check for them */
  608.     if (hit[WILDCHARS[i]]) return(TRUE);
  609.   return(FALSE);
  610. }
  611.  
  612. /*
  613.  * check if directory is on connected structure
  614.  */
  615. defstruct(dirname)
  616. char *dirname;
  617. {
  618. #ifdef MSDOS
  619.   int i;
  620.   char buf[100];
  621.   
  622.   getcwd(buf,100);            /* get connected dir */
  623.   for(i = 0; i < strlen(dirname); i++)
  624.     if (index(STRUCTTERM,dirname[i]))    /* a structure delimiter? */
  625.       return(toupper(dirname[i-1]) == toupper(buf[0]));
  626. #endif
  627.   return(TRUE);
  628. }
  629.  
  630.  
  631. #if unix
  632. /* 
  633.  * WHOAMI:
  634.  * 1) Get real uid
  635.  * 2) See if the $USER environment variable is set
  636.  * 3) If $USER's uid is the same as realuid, realname is $USER
  637.  * 4) Otherwise get logged in user's name
  638.  * 5) If that name has the same uid as the real uid realname is loginname
  639.  * 6) Otherwise, get a name for realuid from /etc/passwd
  640.  */
  641.  
  642. PASSEDSTATIC
  643. char *
  644. whoami () {
  645.   static char realname[256];        /* user's name */
  646.   static int realuid = -1;        /* user's real uid */
  647.   char loginname[256], envname[256];    /* temp storage */
  648.   char *getlogin(), *getenv(), *c;
  649.   struct passwd *p, *getpwnam(), *getpwuid(), *getpwent();
  650.  
  651.   if (realuid != -1)
  652.     return(realname);
  653.  
  654.   realuid = getuid ();            /* get our uid */
  655.  
  656.   /* how about $USER? */
  657.   if ((c = getenv("USER")) != NULL) {    /* check the env variable */
  658.     strcpy (envname, c);        
  659.     p = getpwnam(envname);
  660.     if (p->pw_uid == realuid) { /* get passwd entry */
  661.                     /* for envname */
  662.       strcpy (realname, envname);    /* if the uid's are the same */
  663.       return(realname);
  664.     }
  665.   }
  666.  
  667.   /* can we use loginname() ? */
  668.   if ((c =  getlogin()) != NULL) {    /* name from utmp file */
  669.     strcpy (loginname, c);    
  670.     if ((p = getpwnam(loginname)) != NULL) /* get passwd entry */
  671.       if (p->pw_uid == realuid) {    /* for loginname */ 
  672.     strcpy (realname, loginname);    /* if the uid's are the same */
  673.     return(realname);
  674.       }
  675.   }
  676.  
  677.   /* Use first name we get for realuid */
  678.   if ((p = getpwuid(realuid)) == NULL) { /* name for uid */
  679.     realname[0] = '\0';            /* no user name */
  680.     realuid = -1;
  681.     return(NULL);
  682.   }
  683.   strcpy (realname, p->pw_name);    
  684.   return(realname);
  685. }
  686. #endif
  687.  
  688. /*
  689.  * expand ~user to the user's home directory.
  690.  */
  691. char *
  692. tilde_expand(dirname) char *dirname; {
  693. #ifdef MSDOS
  694.   return(dirname);            /* no users in msdos */
  695. #endif /*  MSDOS */
  696. #if unix
  697.   struct passwd *user, *getpwuid(), *getpwnam();
  698.   static char olddir[BUFLEN];
  699.   static char oldrealdir[BUFLEN];
  700.   static char temp[BUFLEN];
  701.   int i;
  702.   char *whoami();
  703.  
  704.   if (dirname[0] != '~') return(dirname); /* not a tilde...return param */
  705.   if (!strcmp(olddir,dirname)) return(oldrealdir); /* same as last time. */
  706.                     /* so return old answer */
  707.   else {
  708.     for (i = 0; i < strlen(dirname); i++) /* find username part of string */
  709.       if (!index(DIRSEP,dirname[i]))
  710.     temp[i] = dirname[i];
  711.       else break;
  712.     temp[i] = '\0';            /* tie off with a NULL */
  713.     if (strlen(temp) == 1) {        /* if just a "~" */
  714.       user = getpwnam(whoami());    /*  get info on current user */
  715.     }
  716.     else {
  717.       user = getpwnam(&temp[1]);    /* otherwise on the specified user */
  718.     }
  719.   }
  720.   if (user != NULL) {            /* valid user? */
  721.     strcpy(olddir, dirname);        /* remember the directory */
  722.     strcpy(oldrealdir,user->pw_dir);    /* and their home directory */
  723.     strcat(oldrealdir,&dirname[i]);
  724.     return(oldrealdir);
  725.   }
  726.   else {                /* invalid? */
  727.     strcpy(olddir, dirname);        /* remember for next time */
  728.     strcpy(oldrealdir, dirname);
  729.     return(oldrealdir);
  730.   }
  731. #endif
  732. }
  733.  
  734. #ifdef notdef
  735. bcopy(src,dest,len)
  736. register char *src,*dest;
  737. register int len;
  738. {
  739.   if (src > dest)               /* Could source be overwritten? */
  740.     for(; len > 0; len--)       /* No */
  741.       *dest++ = *src++;         /* Simple forward copy */
  742.   else
  743.     for(; len >= 0; len--)       /* Yes */
  744.       *(dest+len) = *(src+len); /* Backwards copy */
  745. }
  746.  
  747. #endif
  748.  
  749. #ifdef NODIRLIB
  750.  
  751. #ifdef opendir
  752. #undef opendir
  753. #endif
  754. DIR *
  755. opendir(name) {
  756.     DIR *dir;
  757.     int fd,open();
  758.     struct stat sbuf;
  759.     int x;
  760.  
  761.     if (stat(name,&sbuf) != 0) return(NULL);
  762.     if (!(sbuf.st_mode & S_IFDIR)) {
  763.     errno = ENOTDIR;
  764.     return(NULL);
  765.     }
  766.     fd = open(name,O_RDONLY,0);
  767.     if (fd == -1)
  768.     perror("open");
  769.     if (fd == -1) return(NULL);
  770.     if ((dir = (DIR *)malloc(sizeof(DIR))) == NULL) {
  771.     close(fd);
  772.     return(NULL);
  773.     }
  774.     dir->size = sbuf.st_size;
  775.     if ((dir->files = (struct direct *) malloc(dir->size)) == 0) {
  776.     close(fd);
  777.     free(dir);
  778.     return(NULL);
  779.     }
  780.     x = read(fd,dir->files,dir->size);
  781.     if (x == -1) perror("read");
  782.     dir->current = dir->files;
  783.     close(fd);
  784.     return(dir);
  785. }
  786.  
  787. struct direct *
  788. readdir(dirp) DIR *dirp; {
  789.     int s = dirp->size / sizeof(struct direct);
  790.     struct direct *temp;
  791.     if (dirp->current >= dirp->files + s)
  792.     return(NULL);
  793.     while (dirp->current->d_ino == 0)
  794.     if (dirp->current >= dirp->files + s)
  795.         return(NULL);
  796.     else dirp->current++;
  797.         if (dirp->current >= dirp->files + s)
  798.     return(NULL);
  799.     temp = dirp->current++;
  800.     return(temp);
  801. }
  802.     
  803. rewinddir(dirp) DIR *dirp; {
  804.     dirp->current = dirp->files;
  805. }
  806.  
  807. closedir(dirp) DIR *dirp;
  808. {
  809.     free(dirp->files);
  810.     free(dirp);
  811. }
  812. #endif
  813.